package problems.producerconsumer;

import problems.BenchmarkSemaphore;
import helpers.IAction;

public class ProducerConsumerWithRaceCondition extends AbstractProducerConsumer {
    private BenchmarkSemaphore items = new BenchmarkSemaphore(0);
    private BenchmarkSemaphore _mutex = new BenchmarkSemaphore(1);

    @Override
    public void consume()
    {
        items.waitOne();
        _mutex.waitOne();
        IAction action = getBuffer().peek();
        _mutex.release();
        getBuffer().remove();
        action.invoke();
    }

    @Override
    public void produce(IAction action)
    {
    	_mutex.waitOne();
        getBuffer().add(action);
        _mutex.release();
        items.release();
    }
}
